"use strict";

module.exports = async function (fastify, options) {
  //我们在card的plugin里将方法注册进了"cardService"里，所以用fastify.cardService()即可获取
  const cardService = fastify.cardService();
  const cardTest = fastify.cardTest();

  //引用fastify自己封装的log
  const log = fastify.log;
  //log级别默认为info，直接直接.level=“等级”修改log在这里的显示级别
  log.level = options.logLevel;

  //根据ID获得一张卡的信息，RESTful风格的url传参，参数前要带“:”
  fastify.get("/:id", async function (request, reply) {
    // const data = cardService.findById(request.params.id);
    return cardService.findById(request.params.id);
    // reply.send(data)
  });

  //根据成员ID获取card[]，不用RESTful风格传参，注意获取参数的方式有不同
  fastify.get("/member", async function (request, reply) {
    const query = request.query;
    return cardService.findByMemberId(query.memberId);
  });

  //新增卡
  fastify.post("/", async function (request, reply) {
    const query = request.query;
    return cardService.insert(query);
  });

  //修改卡
  fastify.put("/", async function (request, reply) {
    const query = request.query;
    return cardService.update(query);
  });

  //删除卡
  fastify.delete("/:id", async function (request, reply) {
    return cardService.destroy(request.params.id);
  });

  fastify.get("/:id/:id2/:id3/:id4", async function (request, reply) {
    log.info("wuhu");
    return null;
  });

  fastify.post("/test", async function (request, reply) {
    const result = cardTest.updateWithTransaction(request.query);
    return result;
  });
};
